import { defineStore } from 'pinia';
import { reqGetCategory1List,reqGetCategory2List,reqGetCategory3List } from '@/api/product/categorySelector';
import type { CategoryStateModel } from '@/api/product/interface/categoryModel';

// 创建store实例

export const useCategoryListStore = defineStore('categoryList', {
  state: () => {
    return {

      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,
      // 下面是三个导航数组项
      category1List: [],
      category2List: [],
      category3List: []
    }
  },
  actions: {
    // 1. 获取一级导航分类数据
    async getCategory1List() {
      const result = await reqGetCategory1List()
      // console.log()
      this.category1List = result
    },
    // 2. 获取二级导航分类数据
    async getCategory2List(category1Id:number) {

      this.category1Id = category1Id

      // 清空2  3id 
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []

      const result = await reqGetCategory2List(category1Id)
      // console.log()
      this.category2List = result
     
    },
     // 3. 获取三级导航分类数据
     async getCategory3List(category2Id:number) {
      this.category2Id = category2Id

      this.category3Id = undefined
      const result = await reqGetCategory3List(category2Id)
      // console.log()
      this.category3List = result
      
    },
    // 4. 设置三级导航id的值
    setCategory3Id(category3Id:number) {
      this.category3Id = category3Id
    }
  }
})